package com.lcq.study.pdf.jasper;

import com.lcq.study.pdf.dto.LightDataDto;
import com.lcq.study.pdf.dto.ModelTableSource;
import com.lcq.study.pdf.dto.PmYCDataDto;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName JasperReportTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/6/24 10:07
 * @Version 1.0
 **/
public class LightReportTest {
    public static void main(String[] args) throws JRException {


        String jasperFilePath = "C:/Users/86156/JaspersoftWorkspace/MyReports";

        String name1 = "BPAReport_8";
        String jasperFile1 = getJasperFile(jasperFilePath, name1);


        String name2 = "BPAReport_9";
        String jasperFile2 = getJasperFile(jasperFilePath, name2);



        ModelTableSource mts = new ModelTableSource();
        mts.setTableData(new JRBeanCollectionDataSource(getDataList()));
        List<ModelTableSource> mlist = new ArrayList<>();
        mlist.add(mts);

        HashMap<String, Object> params1 = new HashMap<>();
        JasperFillManager jasperFillManager = JasperFillManager.getInstance(DefaultJasperReportsContext.getInstance());
        JasperPrint jasperPrint1 = jasperFillManager.fill(jasperFile1, params1, new JREmptyDataSource());

        HashMap<String, Object> params2 = new HashMap<>();
        JasperPrint jasperPrint2 = jasperFillManager.fill(jasperFile2, params2, new JRBeanCollectionDataSource(mlist));

        List<JasperPrint> jasperPrintList = new ArrayList<>();
        jasperPrintList.add(jasperPrint1);
        jasperPrintList.add(jasperPrint2);

        String destFile = "D:/test/pdf/LightReport.pdf";

        JasperUtils.exportPages(destFile, jasperPrintList);
    }

    private static String getJasperFile(String jasperFilePath, String name) {
        return jasperFilePath + "/" + name + ".jasper";
    }

    public static List<LightDataDto> getDataList(){
        ArrayList<LightDataDto> list = new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            LightDataDto lightDataDto = new LightDataDto();
            lightDataDto.setRoomNum(""+i);
            lightDataDto.setRoomVolume("53.46");
            lightDataDto.setCtgd("2.5");
            lightDataDto.setBltgl("0.339");
            lightDataDto.setQtclfsl("0.502");
            lightDataDto.setCgxs("3.46");
            lightDataDto.setZycgzd("259.34");
            list.add(lightDataDto);
        }


        return list;
    }
}
